/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.etop.ondoc.dao;

import com.etop.ondoc.beans.Category;
import com.etop.ondoc.beans.Document;
import com.etop.ondoc.util.CommonsUtil;
import com.etop.ondoc.util.HibernateUtil;
import com.etop.ondoc.util.Pager;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author jansing lz_jansing@163.com
 */
public class DocumentDao {
    
    public void add(Document docu) {
        Session session = null;
        try {
            session = HibernateUtil.openSession();
            session.beginTransaction();
            session.save(docu);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.out.println("DocumentDao--add---" + e.getMessage());
            if (session != null) {
                session.getTransaction().rollback();
            }
            throw e;
        } finally {
            HibernateUtil.close(session);
        }
    }
    
    public Document load(int id) {
        Session session = HibernateUtil.openSession();
        Document docu = (Document) session.get(Document.class, id);
        HibernateUtil.close(session);
        return docu;
    }
    
    public void update(Document docu) {
        Session session = null;
        try {
            session = HibernateUtil.openSession();
            session.beginTransaction();
            session.update(docu);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.out.println("DocumentDao---update---" + e.getMessage());
            if (session != null) {
                session.getTransaction().rollback();
            }
            throw e;
        } finally {
            HibernateUtil.close(session);
        }
    }

    public void delete(Document docu) {
        Session session = null;
        try {
            session = HibernateUtil.openSession();
            session.beginTransaction();
//            docu.setIsVaild(Document.IS_NOT_VAILD);
            session.delete(docu);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.out.println("DocumentDao---delete---" + e.getMessage());
            if (session != null) {
                session.getTransaction().rollback();
            }
            throw e;
        } finally {
            HibernateUtil.close(session);
        }
    }

    public List<Document> list() {
        List<Document> list = null;
        Session session = HibernateUtil.openSession();
        list = session.createQuery("from Document").list();
        list.get(1);
        HibernateUtil.close(session);
        return list;
    }

    /**
     * 分页取数据
     *
     * @param offset 偏移量
     * @param pagesize 页大小
     * @return
     */
    public Pager<Document> pager(int offset, int pagesize) {
        Session session = HibernateUtil.openSession();
        Pager<Document> pages = null;
        try {
            pages = new Pager<>();
            
            List<Document> datas = session.createQuery("from Document").setFirstResult(offset).setMaxResults(pagesize).list();
            int totalRecord = ((BigInteger) session.createSQLQuery("select count(*) from t_document").uniqueResult()).intValue();
            
            pages.setDatas(datas);
            pages.setPageOffset(offset);
            pages.setPageSize(pagesize);
            pages.setTotalRecord(totalRecord);
        } finally {
            HibernateUtil.close(session);
        }
        return pages;
    }

    /**
     * 分页按分类取数据
     *
     * @param offset 偏移量
     * @param pagesize 页大小
     * @param cate 所属分类
     * @return
     */
    public Pager<Document> pager(int offset, int pagesize, Category cate) {
        Session session = HibernateUtil.openSession();
        Set<Category> allSubCategorys = CommonsUtil.getAllSubCategorys(cate);
        if (cate.getCateSet().isEmpty()) {
            allSubCategorys.add(cate);
        }
        List<Document> list = session
                .createCriteria(Document.class)
                .add(Restrictions.in("category", allSubCategorys))
                .setFirstResult(offset)
                .setMaxResults(pagesize)
                .list();
        int totalRecord = ((Number) session.createCriteria(Document.class)
                .setProjection(Projections.rowCount())
                .add(Restrictions.in("category", allSubCategorys))
                .uniqueResult()).intValue();
        Pager<Document> pages = new Pager();
        pages.setDatas(list);
        pages.setPageOffset(offset);
        pages.setPageSize(pagesize);
        pages.setTotalRecord(totalRecord);
        return pages;
    }
}
